package com.mk.es.service.entity;

import java.io.Serializable;
import java.util.HashMap;

/**
 * ES查询参数
 */
public class EsQueryParam extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = -7047037081506758422L;

    /**
     * 当前页
     */
    private int pageIndex = 1;

    /**
     * 每页的数量
     */
    private int pageSize = 20;

    /**
     * 查询内容
     */
    private String queryStr;


    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getQueryStr() {
        return queryStr;
    }

    public void setQueryStr(String queryStr) {
        this.queryStr = queryStr;
    }

    @Override
    public Object put(String key, Object value) {
        if ("pageIndex".equals(key)) {
            this.setPageIndex(Integer.parseInt(String.valueOf(value)));
            return value;
        } else if ("pageSize".equals(key)) {
            this.setPageSize(Integer.parseInt(String.valueOf(value)));
            return value;
        } else if ("queryStr".equals(key)) {
            this.setQueryStr(String.valueOf(value));
            return value;
        } else {
            return super.put(key, value);
        }
    }

    public String getString(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }

        return String.valueOf(value);
    }

    public int getInt(String key) {
        return getInteger(key, 0);
    }

    public Integer getInteger(String key, Integer defaultValue) {
        Object value = this.get(key);
        if (value == null) {
            return defaultValue;
        }

        try {
            return Integer.getInteger(String.valueOf(value));
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
    }

    public long getLong(String key) {
       return getLong(key, 0L);
    }

    public Long getLong(String key, Long defaultValue) {
        Object value = this.get(key);
        if (value == null) {
            return defaultValue;
        }

        try {
            return Long.parseLong(String.valueOf(value));
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
    }
}
